# -*- coding: utf-8 -*-


import urllib2
import logging

log = logging.getLogger(__name__)

FEED_TYPES = ['application/atom+xml', 'application/rdf+xml', 'application/rss+xml', 'application/x-netcdf', 'application/xml', 'text/xml']

def identify(query):

    query_url = False
  
    for dom in ['.com', '.edu', '.gov', '.ru', '.ua', '.in', '.net', '.org',
                '.info ', '.biz', '.us', '.ws', '.cc', '.bz', '.tv', '.cn',
                '.mobi'] :
        if dom in query:
            query_url = True    
            break

    if query_url:
        url = query
        if url[:7] != 'http://':
            url = 'http://' + url
    
        log.info('Identifying url = %s', url)

        try:
            opened_url = urllib2.urlopen(url)
        except urllib2.HTTPError, e:
            log.error(e)
            raise e

        if opened_url.headers.type == 'text/html':
            return 'html'
        elif opened_url.headers.type in FEED_TYPES:
            return 'feed'
        return opened_url.headers.type
    else:
        return "title"
        

